package com.weita.lightfood.controller;


import com.weita.lightfood.model.query.dish.AddDishQuery;
import com.weita.lightfood.model.query.dish.UpdateTemplateQuery;
import com.weita.lightfood.model.vo.dish.DishDetailsVo;
import com.weita.lightfood.model.vo.dish.DishTemplateDetailsVo;
import com.weita.lightfood.model.vo.dish.ShowIndexVo;
import com.weita.lightfood.model.vo.material.MateralNameNuvalueVo;
import com.weita.lightfood.service.DishService;
import com.weita.lightfood.service.OrderService;
import com.weita.lightfood.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author IT_Monster at 2019/03/28 14:24.
 * 菜单管理接口
 */
@Api(tags = "菜品管理接口")
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private OrderService orderService;
    /**
     *  通过菜品id获取精选食材食材名和营养价值介绍
     */
    @ApiOperation("通过菜品id获取其精选食材食材名和介绍")
    @PostMapping("/getMaterialByDishId")
    @ApiImplicitParam(name = "dishId", value = "菜品id", defaultValue = "666")
    public Result<List<MateralNameNuvalueVo>> getMaterialByMenuId(int dishId) {
        try {
            return Result.success(dishService.getMaterialByMenuId(dishId));
        } catch (Exception e) {
            return Result.fail("获取精选食材名和介绍失败");
        }
    }

    /**
     * 根据用户和日期推荐菜品
     */
    @ApiOperation("根据用户和日期获取当天一日三餐")
    @PostMapping("/showIndex")
    @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "45")
    public Result<Map<String, ShowIndexVo>> showIndex(int userId, String date) {
        try {
            return Result.success(dishService.showIndex(userId, date));
        } catch (Exception e) {
            return Result.fail("当天暂无菜品推荐");
        }
    }

    /**
     * 获取所有模板
     * @return
     */
    @ApiOperation("获取所有模板")
    @PostMapping("/getAllDishTemplate")
    public Result<List<DishTemplateDetailsVo>> getAllDishTemplate(){
        try {
            return Result.success(dishService.getAllTemplate());
        } catch (Exception e) {
            return Result.fail("获取模板失败");
        }
    }


    /**
     * 发布菜品，并保存到历史菜单，以便展示在页面,接受图片
     * @param addDishQuery
     * @param img
     * @return
     */
    @ApiOperation("发布菜品")
    @PostMapping("/publishDishWithImg")
    @ApiImplicitParams({
            @ApiImplicitParam(name="state", value = "菜品早0中1晚2", defaultValue = "0"),
            @ApiImplicitParam(name="calorie", value = "卡路里含量", defaultValue = "0"),
            @ApiImplicitParam(name="protein", value = "蛋白质含量", defaultValue = "0"),
            @ApiImplicitParam(name="fat", value = "脂肪含量", defaultValue = "0"),
            @ApiImplicitParam(name="price", value = "菜品单价", defaultValue = "0.0"),
            @ApiImplicitParam(name="carbohydrate", value = "碳水化合物含量", defaultValue = "0")
    })
    public Result<String> publishDishWithImg(AddDishQuery addDishQuery , MultipartFile img) {
        try {
            return Result.success(dishService.publishDishWithImg(addDishQuery,img));
        } catch (Exception e) {
            return Result.fail("发布菜品时出现错误");
        }
    }


    /**
     *  删除模板
     * @param dishId
     * @return
     */
    @ApiOperation("删除模板")
    @PostMapping("/deleteTemplate")
    @ApiImplicitParam(name = "dishId", value = "菜品id", defaultValue = "0")
    public Result<String> deleteTemplate(int dishId){
        try {
            return Result.success(dishService.deleteTemplate(dishId));
        } catch (Exception e) {
            return Result.fail("删除模板出现错误");
        }
    }

    /**
     * 通过模板名搜索模板
     * @param name
     * @return
     */
    @ApiOperation("通过模板名搜索模板")
    @PostMapping("/searchTemplate")
    public Result<List<DishTemplateDetailsVo>> searchTemplate(String name){
        try {
            return Result.success(dishService.searchTemplate(name));
        } catch (Exception e) {
            return Result.fail("获取模板出错");
        }
    }


    /**
     * 修改模板
     * @param updateTemplateQuery
     * @param img
     * @return
     */
    @ApiOperation("修改模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name="state", value = "菜品早0中1晚2", defaultValue = "0"),
            @ApiImplicitParam(name="calorie", value = "卡路里含量", defaultValue = "0"),
            @ApiImplicitParam(name="protein", value = "蛋白质含量", defaultValue = "0"),
            @ApiImplicitParam(name="fat", value = "脂肪含量", defaultValue = "0"),
            @ApiImplicitParam(name="price", value = "菜品单价", defaultValue = "0.0"),
            @ApiImplicitParam(name="carbohydrate", value = "碳水化合物含量", defaultValue = "0"),
            @ApiImplicitParam(name = "id", value = "菜品id", defaultValue = "0")

    })
    @PostMapping("/updateTemplate")
    public Result<String> updateTemplate(UpdateTemplateQuery updateTemplateQuery, MultipartFile img){
        try {
            return Result.success(dishService.updateTemplate(updateTemplateQuery, img));
        } catch (Exception e) {
            return Result.fail("修改模板出错");
        }
    }

    /**
     * 通过菜谱id获取所有食材
     * @param dishId
     * @return
     */
    @ApiOperation("通过菜谱id获取所有食材名")
    @PostMapping("/getAllMaterialByDishId")
    @ApiImplicitParam(name = "dishId", value = "菜品id", defaultValue = "0")
    public Result<List<String>> getMaterialByDishId(int dishId){
        try {
            return Result.success(dishService.getMaterialByDishId(dishId));
        } catch (Exception e) {
            return Result.fail("获取食材出错");
        }

    }


    /**
     *  获取菜品详情
     * @param userId
     * @param dishId
     * @return
     */
    @ApiOperation("获取菜品详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dishId", value = "菜品id", defaultValue = "0"),
        @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0")
    })
    @PostMapping("/getDishDetails")
    public Result<DishDetailsVo> getDishDetails(int userId, int dishId){
        try {
            return Result.success(orderService.getDishDetails(userId, dishId));
        } catch ( Exception e ) {
            e.printStackTrace();
            return Result.fail("获取菜品详情失败");
        }
    }
}
